
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>体育赛事结果</title>
<style>
    #loading {
    text-align: center;
    margin-top: 10px;
    font-size: 18px;
    color: #333;
}
  :root {
    --primary: #007bff;
    --success: #28a745;
    --text: #333;
  }
  body {
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #f8f9fa;
  }
  .container {
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
    padding: 10px;
  }
  .header {
    background: var(--primary);
    color: white;
    padding: 12px;
    border-radius: 8px 8px 0 0;
    margin-bottom: 8px;
    text-align: center;
  }
  .header h1 {
    margin: 0;
    font-size: 18px;
    font-weight: 500;
  }
  .match-item {
    background: white;
    padding: 12px;
    border-radius: 8px;
    margin-bottom: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    display: flex;
    flex-direction: column;
  }
  .match-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 6px;
  }
  .match-info h2 {
    margin: 0;
    font-size: 16px;
    font-weight: 500;
    color: var(--text);
  }
  .match-info p {
    margin: 0;
    font-size: 14px;
    color: #666;
  }
  .match-time {
    font-size: 12px;
    color: #999;
    margin-top: 8px;
  }
  .recommendation {
    background: #f8f9fa;
    padding: 8px;
    border-radius: 4px;
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 4px;
  }
  .recommendation p {
    margin: 0;
    font-size: 12px;
    color: #666;
  }
  .recommendation .correct {
    color: var(--success);
    font-weight: 500;
  }
  .match-result {
    font-size: 18px;
    font-weight: 600;
    color: var(--primary);
    margin-top: 10px;
    align-self: flex-end;
  }
  @media (max-width: 375px) {
    .match-info h2 {
      font-size: 15px;
    }
    .match-info p {
      font-size: 13px;
    }
    .match-time {
      font-size: 11px;
    }
    .recommendation p {
      font-size: 11px;
    }
  }
       .bottom-nav {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: #fff;
        padding: 8px 0;
        box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
        display: flex;
        justify-content: space-around;
        z-index: 100;
        border-radius: 0 0 8px 8px
    }
</style>
</head>
<body>

<div class="container">
  <div class="header">
    <h1>Early-AI-Model</h1>
    <p style="font-size:12px;margin-top:4px">AI开启随时出单，请注意查收</p >
  </div>


<div id="data-container">
    <!-- 数据将插入到这里 -->
</div>
<div id="loading" style="display:none;">加载中...</div>


</div>

<!-- 新增底部导航 -->
<nav class="bottom-nav">
    <div class="nav-item active">
        <div class="nav-icon">
            <svg viewBox="0 0 24 24">
                <path d="M12 2L2 12h3v8h6v-6h2v6h6v-8h3L12 2z"/>
            </svg>
        </div>
        HOME
    </div>
    <div class="nav-item">
        <div class="nav-icon">
            <svg viewBox="0 0 24 24">
                <path d="M14 3v1h-4V3h4m-1 12l5-5-5-5v3H8v4h5v3z"/>
                <path d="M19 13h-4v3l-5-5 5-5v3h4v4z"/>
            </svg>
        </div>
        REPORT
    </div>
    <div class="nav-item">
        <div class="nav-icon">
            <svg viewBox="0 0 24 24">
                <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 17h-2v-2h2v2zm2.07-7.75l-.9.92C13.45 12.9 13 13.5 13 15h-2v-.5c0-1.1.45-2.1 1.17-2.83l1.24-1.26c.37-.36.59-.86.59-1.41 0-1.1-.9-2-2-2s-2 .9-2 2H8c0-2.21 1.79-4 4-4s4 1.79 4 4c0 .88-.36 1.68-.93 2.25z"/>
            </svg>
        </div>
        HELP
    </div>
    <div class="nav-item">
        <div class="nav-icon">
            <svg viewBox="0 0 24 24">
                <path d="M12 5.9c1.16 0 2.1.94 2.1 2.1s-.94 2.1-2.1 2.1S9.9 9.16 9.9 8s.94-2.1 2.1-2.1m0 9c2.97 0 6.1 1.46 6.1 2.1v1.1H5.9V17c0-.64 3.13-2.1 6.1-2.1M12 4C9.79 4 8 5.79 8 8s1.79 4 4 4 4-1.79 4-4-1.79-4-4-4zm0 9c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
            </svg>
        </div>
        LOGIN
    </div>
</nav>
<!-- 在body结束前添加登录模态框 -->
<div id="loginModal" style="display:none; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.5); z-index:999">
  <div style="position:relative; background:white; width:300px; margin:100px auto; padding:20px; border-radius:8px">
    <button id="closeLogin" style="position:absolute; top:10px; right:10px; background:none; border:none; font-size:20px">&times;</button>
    <h2 style="margin:0 0 20px; text-align:center; color:var(--text)">用户登录</h2>
    <form id="loginForm">
      <input type="text" placeholder="用户名" style="width:100%; margin-bottom:10px; padding:8px; border:1px solid #ddd; border-radius:4px">
      <input type="password" placeholder="密码" style="width:100%; margin-bottom:15px; padding:8px; border:1px solid #ddd; border-radius:4px">
      <button type="submit" style="width:100%; padding:10px; background:var(--primary); color:white; border:none; border-radius:4px">登录</button>
    </form>
  </div>
</div>

</body>
<script>
// 配置参数
const totalPages = 10; // 最大加载页数
let currentPage = 1; // 当前页码
const dataContainer = document.getElementById('data-container'); // 数据展示容器
const loadingElement = document.getElementById('loading'); // 加载提示元素

// 加载数据的函数
async function loadData(page) {
    loadingElement.style.display = 'block'; // 显示加载提示
    try {
        const response = await fetch(`/matchs?page=${page}`);
        if (!response.ok) {
            throw new Error('Network response was not ok');
        }
        const data = await response.json();
        displayData(data);
    } catch (error) {
        console.error('Fetch error:', error);
        alert('加载数据时出错，请重试。');
    } finally {
        loadingElement.style.display = 'none'; // 隐藏加载提示
    }
}

// 展示数据的函数
function displayData(data) {
    data.forEach(item => {
        console.log(item);
        const div = document.createElement('div');
        time = timestampToLocalTime(item.start_time)
        //div.textContent = item; // 假设数据有content字段
        rf='';
       if (item.performance === null || item.performance === undefined || item.performance === '') {
            rf = '';
        } else if (item.performance.charAt(0) === 'W') {
            rf = '√';
        } else if (item.performance.charAt(0) === 'P') {
            rf = 'PUSH';
        } else {
            rf = 'x';
        }


        div.innerHTML = `<div class="match-item"><div class="match-info"><h2>${item.league}</h2><p>${item.home} - ${item.away}</p ><div class="recommendation"><p>Prediction：${item.prediction}<span class="correct">${rf} ${item.score}</span></p ></div></div><div class="match-time">Match Time：${time}</div><div class="match-result"></div></div>`

        dataContainer.appendChild(div);
    });
}

// 滚动事件处理函数
function handleScroll() {
    if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - 50) {
        // 滚动到底部，加载下一页
        if (currentPage < totalPages) {
            currentPage++;
            loadData(currentPage);
        } else {
            console.log('已加载所有数据');
        }
    }
}

// 节流函数，防止频繁触发
function throttle(func, wait) {
    let timeout = null;
    return function() {
        if (!timeout) {
            timeout = setTimeout(() => {
                func.apply(this, arguments);
                timeout = null;
            }, wait);
        }
    };
}
function timestampToLocalTime(timestamp) {
    if (typeof timestamp !== 'number' || isNaN(timestamp)) {
        throw new Error('Invalid timestamp');
    }
    let date = new Date(timestamp);
    if (isNaN(date.getTime())) {
        throw new Error('Invalid date');
    }
    let year = date.getFullYear();
    let month = pad(date.getMonth() + 1);
    let day = pad(date.getDate());
    let hours = pad(date.getHours());
    let minutes = pad(date.getMinutes());

    return `${year}-${month}-${day} ${hours}:${minutes}`;
}

function pad(num) {
    return num.toString().padStart(2, '0');
}

// Example usage:
let timestamp = 1630456800000;
try {
    console.log(timestampToLocalTime(timestamp));  // Output: "2021-09-01 00:00:00"
} catch (error) {
    console.error(error.message);
}

// 初始化加载第一页
loadData(currentPage);

// 添加滚动事件监听器，使用节流优化
window.addEventListener('scroll', throttle(handleScroll, 500));
</script>
<script>
// 添加登录状态检查函数
function isLoggedIn() {
  return !!localStorage.getItem('authToken'); // 假设使用localStorage存储token
}

// 登录模态框控制函数
function showLoginModal() {
  const modal = document.getElementById('loginModal');
  modal.style.display = 'block';
}

function hideLoginModal() {
  const modal = document.getElementById('loginModal');
  modal.style.display = 'none';
}

// 登录表单处理
document.getElementById('loginForm')?.addEventListener('submit', async (e) => {
  e.preventDefault();

    const formData = new URLSearchParams();
  formData.append('username', e.target[0].value);
  formData.append('password',  e.target[1].value);

  try {
    const response = await fetch('/token', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: formData
    });

    if (response.ok) {

      const data = await response.json();
      console.log(data);
      localStorage.setItem('authToken', data.token);
      hideLoginModal();
      //window.location.href = '/';
    } else {
      alert('登录失败，请检查用户名密码');
    }
  } catch (error) {
    console.error('登录错误:', error);
    alert('网络错误，请稍后重试');
  }
});

// 绑定登录按钮点击事件
document.querySelectorAll('.nav-item')[3].addEventListener('click', () => {
  if (isLoggedIn()) {
    window.location.href = '/profile';
  } else {
    showLoginModal();
  }
});

// 绑定关闭按钮
document.getElementById('closeLogin')?.addEventListener('click', hideLoginModal);

// 点击模态框背景关闭
document.getElementById('loginModal')?.addEventListener('click', (e) => {
  if (e.target === document.getElementById('loginModal')) {
    hideLoginModal();
  }
});
</script>
</html>
